﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ParieurPro.DAL;
using ParieurPro.DAL.DatabaseModel;

namespace MyBet.Web.Controls
{
    public partial class CustomLogin : System.Web.UI.UserControl
    {
        public UserObject CurrentUser
        {
            get
            {
                if (Session["CurrentUser"] != null)
                {
                    return Session["CurrentUser"] as UserObject;
                }
                return null;
            }

            set
            {
                Session["CurrentUser"] = value;
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request.Cookies["lastlogin"] != null)
            {
                LoginControl.UserName = Server.HtmlEncode(Request.Cookies["lastlogin"].Value);
            }

            LoginControl.LoginButtonText = "Se connecter";
            LoginControl.UserNameLabelText = "Login";
            LoginControl.PasswordLabelText = "Mot de passe";
            VersionLabel.Text = String.Format("Version {0}", MyBet.Web.Properties.Resources.SmartphoneVersion);
        }

        /// <summary>
        /// Chargement des données de l'utilisateur après authentification
        /// de l'utilisateur
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void LoginControl_LoggedIn(object sender, EventArgs e)
        {
            // Set cookie value
            using (MyBetDBEntities dbContext = new MyBetDBEntities())
            {
                Login currentSender = sender as Login;
                String currentName = currentSender.UserName;
                HttpCookie aCookie = new HttpCookie("lastlogin");
                aCookie.Value = currentName;
                aCookie.Expires = DateTime.Now.AddMonths(6);
                Response.Cookies.Add(aCookie);
                CurrentUser = UserObject.LoadUser(dbContext, currentName);
            }
        }
    }
}